package com.sentiment.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.Document;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sentiment.database.bean.Role;
import com.sentiment.database.bean.User;
import com.sentiment.database.bean.UserInfo;
import com.sentiment.database.bean.User_Roles;
import com.sentiment.database.dbUtil.DbUtil;
import com.sentiment.tools.Format;

public class UserList {
	
	//用户id
	private String user_id;
	//用户名
	private String username;
	//用户注册日期
	private Date registered_date;
	//用户角色
	private List<String> role;
	//用户状态
	private boolean status;
	
	public String getUser_id() {
		return user_id;
	}
	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getRegistered_date() {
		return Format.date2String(registered_date,3);
	}
	public void setRegistered_date(Date registered_date) {
		this.registered_date = registered_date;
	}
	public List<String> getRole() {
		return role;
	}
	public void setRole(List<String> role) {
		this.role = role;
	}
	public boolean getStatus() {
		return status;
	}
	public void setStatus(boolean status) {
		this.status = status;
	}
	public UserList(String user_id, String username, Date registered_date, List<String> role,
			boolean status) {
		super();
		this.user_id = user_id;
		this.username = username;
		this.registered_date = registered_date;
		this.role = role;
		this.status = status;
	}
	public UserList() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 分页从数据库中查询 UserList 信息
	 * @param page 页数
	 * @param count 每页数据条数
	 * @return
	 */
	public List<UserList> getUserList(int page, int count) {
		
		List<UserList> users = new ArrayList<>();
		DbUtil dbutil = new DbUtil();
		dbutil.openConnection(User.dbName, User.collection);
		User user = new User();
		UserList userlist = new UserList();
		String user_id;
		Document t = new Document();
		List<String> roles_id = new ArrayList<>();
		List<Role> roles = new ArrayList<>();
		
		List<Document> tlist = dbutil.getPage(page, count);
		dbutil.closeConnection();
		for(int i = 0; i < tlist.size(); i++){
			List<String> rolesdetail = new ArrayList<>();
			user = new User(tlist.get(i));
			user_id = user.get_id();
			
			roles = user.getRolesByUser_id(user_id);
			
			for(int j = 0; j < roles.size(); j++){
				rolesdetail.add(roles.get(j).getRole_detail());
			}
			
			UserInfo info = new UserInfo();
			info = info.getUserInfoByUser_id(user_id);
			
			//返回用户昵称
			userlist = new UserList(user.get_id(), user.getUsercode(), Format.string2Date(info.getRigisterdate(),5), rolesdetail, user.isLocked());
			
			users.add(userlist);
		}
		return users;
	}
	
	/**
	 * 返回 User 记录条数
	 * @return
	 */
	@JsonIgnore
	public long getCount(){
		
		return new User().getCount();
		
	}
	
	/**
	 * 根据 user 返回userlist
	 * @param user
	 * @return
	 */
	@JsonIgnore
	public UserList getUserListByUser(User user){
		UserList userlist = new UserList();
		userlist.setUser_id(user.get_id());
		userlist.setUsername(user.getUsercode());
		userlist.setStatus(user.isLocked());
		
		List<Role> rolelist = user.getRolesByUser_id(user.get_id());
		List<String> role = new ArrayList<>();
		for(int i = 0; i < rolelist.size(); i++){
			role.add(rolelist.get(i).getRole_detail());
		}		
		userlist.setRole(role);
		
		UserInfo info = new UserInfo();
		info = info.getUserInfoByUser_id(user.get_id());
		userlist.setRegistered_date(Format.string2Date(info.getRigisterdate(), 3));
		return userlist;
	}
	
	
	@Override
	public String toString() {
		return "UserList [user_id=" + user_id + ", username=" + username
				+ ", registered_date=" + registered_date + ", role=" + role
				+ ", status=" + status + "]";
	}
	
	public static void main(String[] args) {
		
		UserList userlist = new UserList();
		List<UserList> users = userlist.getUserList(1,10);
		
//		System.out.println(users.size());
//		for(int i = 0; i < users.size(); i++){
//			System.out.println(users.get(i).toString());
//		}
		
		ObjectMapper mapper = new ObjectMapper();
		String jsonArray = "";
		try {
			jsonArray = mapper.writeValueAsString(users);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		String jsonStr = "\"users\":" + jsonArray;
		System.out.println(jsonStr);
		
	}
	
}
